package com.example.study1206.Utils;

import com.example.study1206.Entity.Attribute;

import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface DataBaseMapper { ;

    @Insert("INSERT INTO ${tableName}(${attributes}) VALUES (${values})")
    public void bigInsert(@Param("tableName") String tableName, @Param("attributes") String attributes, @Param("values") String values);

    @Select("CREATE TABLE IF NOT EXISTS ${tableName}(${details})DEFAULT CHARSET=UTF8;")
    public void creatTable(@Param("tableName") String tableName, @Param("details") String details);

    @Update("UPDATE ${tableName} SET ${details} where ${condition}")
    public void bigUpdate(@Param("tableName") String tableName, @Param("details") String details, @Param("condition") String condition);

    @Delete("DELETE FROM ${tableName} where ${condition}")
    public void bigDelete(@Param("tableName") String tableName, @Param("condition") String condition);

    @Select("SELECT IFNULL((SELECT 'Y' from ${tableName} where ${condition} limit 1),'N')")
    public String detect(@Param("tableName") String tableName, @Param("condition") String condition);

    @Select("SHOW COLUMNS FROM ${tableName}")
    public Attribute[] getAttributes(@Param("tableName") String tableName);

    @Select("alter Table ${tableName} modify Column ${columnName} varchar(${len})")
    public void alterAColumn(@Param("tableName") String tableName, @Param("columnName") String columnName, @Param("len") int len);

    @Select("SELECT table_name FROM information_schema.TABLES WHERE table_name = #{tableName}")
    public String findTable(@Param("tableName") String tableName);

    @Select("Select ${attributes} from ${tableName} ${condition} ORDER BY\n" +
            "(   SELECT GROUP_CONCAT(`COLUMN_NAME` SEPARATOR ', ')\n" +
            "    FROM `information_schema`.`COLUMNS`\n" +
            "    WHERE (`TABLE_SCHEMA` = 'test')\n" +
            "      AND (`TABLE_NAME` = '${tableName}')\n" +
            "      AND (`COLUMN_KEY` = 'PRI')\n" +
            ")${pageInfo}")
    public List<Map<String, String>> bigSelect(@Param("attributes") String attributes, @Param("tableName") String tableName, @Param("condition") String condition, @Param("pageInfo") String pageInfo);
}
